package cn.itsource.ymcc.consumer;

import cn.itsource.ymcc.constant.OrderStatusConstants;
import cn.itsource.ymcc.domain.PayOrder;
import cn.itsource.ymcc.dto.PayOrderDto;
import cn.itsource.ymcc.mapper.PayOrderMapper;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Component
@RocketMQMessageListener(
        consumerGroup = "CoureseOrderPayConsumer",
        topic = OrderStatusConstants.MQ_TOPIC_COURSE_ORDER_PUSH_TO_TRANSACTION,
        selectorExpression = OrderStatusConstants.MQ_TAGS_COURSE_ORDER_PUSH_TO_TRANSACTION
)
@Slf4j
public class CoureseOrderPayConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private PayOrderMapper payOrderMapper;
    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            byte[] body = messageExt.getBody();
            String s = new String(body, "utf-8");
            PayOrderDto payOrderDto = JSONObject.parseObject(s, PayOrderDto.class);
            PayOrder payOrder = new PayOrder();
            BeanUtils.copyProperties(payOrderDto,payOrder);
            payOrderMapper.insert(payOrder);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
